//
//  PCShowPhotoCommVC.m
//  PhotoCircle
//
//  Created by wujungao on 12/15/15.
//  Copyright © 2015 wujungao. All rights reserved.
//

#import "PCShowPhotoCommVC.h"
#import "photoCommCell.h"
#import "UIImageView+WebCache.h"
#import "fetchData.h"
#import "GMString.h"
#import "PCCommentController.h"
#import "PCMeStore.h"
#import "PCUserInfoManager.h"
#import "PCUserModel.h"
#import "PCUserLoginViewController.h"
#import "GMString.h"
#import "ShareSDKHelper.h"
#import "photoExhibitionDetailModel.h"
#import "photosModel.h"
#import "gameDetailModel.h"
#import "imagePropertyModel.h"
#import "imagePropertyView.h"
#import "shareModel.h"
#import "GMTipsHelper.h"
#import "PCUserModel.h"

//ShareSDK头文件
#import <ShareSDK/ShareSDK.h>

#define bottomViewH 49
#define imagePropertyViewH 361
static NSString *cellID=@"cellID";
static BOOL isClickScaled=NO;

@interface PCShowPhotoCommVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descConstraintH;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITextView *desc;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;
@property (weak, nonatomic) IBOutlet UILabel *imageNumLabel;
@property (weak, nonatomic) IBOutlet UIButton *btnA;
@property (weak, nonatomic) IBOutlet UIButton *btnB;
@property (weak, nonatomic) IBOutlet UIButton *imagePropertyBtn;

- (IBAction)backAction:(UIButton *)sender;
- (IBAction)btnAAction:(UIButton *)sender;
- (IBAction)btnBCommentListAction:(UIButton *)sender;
- (IBAction)imagePropertyBtnAction:(UIButton *)sender;

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,assign)vcType vcstyle;
@property(nonatomic,strong)NSString *token;
@property(nonatomic,strong)photoCommCell *cell;
@property(nonatomic,assign)CGRect backRect;
@property(nonatomic,strong)UIView *whileLineView;

//photoExhibitionDetailVC
@property(nonatomic,strong)photoExhibitionDetailModel *photoExhibitionDetailModel;
@property(nonatomic,strong)NSString *photoExhibitionDetailUUID;
@property(nonatomic,assign)NSInteger commentCount;
@property(nonatomic,strong)NSMutableArray *photosURLArray;
@property(nonatomic,strong)NSArray *photosDictArray;

//photoMatchDetailVC
@property(nonatomic,strong)NSString *photoMatchDetailUUID;
@property(nonatomic,strong)gameDetailModel *photoMatchDetailModel;

//imagePropertyModel
@property(nonatomic,strong)imagePropertyModel *imagePropertyModel;
@property(nonatomic,strong)imagePropertyView *imagePropertyView;

@end

@implementation PCShowPhotoCommVC
#pragma mark - PCShowPhotoCommVC method
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self configNavAndTabBarAndBottomView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor=[UIColor blackColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPhotoCommentCount) name:PHOTOCOMMENTCOUNT_NOTIFICATION_NAME object:nil];
    
    [self configTopView];
    [self configBottomView];
    [self configCollectionView];
    [self configTextField];
    [self configImagePropertyView];
    
    if(self.vcstyle==photoExhibitionDetailVC){
        
        [self fetchPhotoExhibitionDetail];

    }else if (self.vcstyle==photoAlbumVC){
        
        [self fetchPhotoUserAlbumDetail];
        
    }else{
        
        [self fetchphotoMatchDetail];
    }
}

-(void)addPhotoCommentCount
{
    self.commentCount ++;
    [self.btnB setTitle:[NSString stringWithFormat:@"%ld跟贴",(long)self.commentCount] forState:UIControlStateNormal];
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    UIView *barView=self.navigationController.navigationBar;
    for(UIView *v in barView.subviews){
        if([v isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]){
            v.hidden=NO;
        }
    }
    self.navigationController.navigationBar.translucent = NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    [GMTipsHelper showDismiss];
}

-(void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - from other VC pass
-(void)passImageURLArray:(NSArray *)imageURLArray{
    
    self.photosURLArray=[imageURLArray mutableCopy];
}

-(void)passVCType:(vcType)vcstyle{
    
    self.vcstyle=vcstyle;
}

-(void)passphotoExhibitionDetailUUID:(NSString *)uuid{
    
    self.photoExhibitionDetailUUID=uuid;
}

-(void)passPhotoMatchUUID:(NSString *)uuid{
    
    self.photoMatchDetailUUID=uuid;
}

#pragma mark - config views
-(void)configNavAndTabBarAndBottomView{
    
    MCHiddenBottomView();
    MCHiddenTabBar();
    MCHiddenPlusBackgroundView();
    MCShowNavBar();
    MCShowNavBackButton();
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    UIView *barView=self.navigationController.navigationBar;
    for(UIView *v in barView.subviews){
        if([v isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]){
            v.hidden=YES;
            v.backgroundColor = [UIColor orangeColor];
        }
    }

    // 透明度设置
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];

    // 设置为半透明
    self.navigationController.navigationBar.translucent = YES;
}

-(void)configTopView{
    
    self.topView.backgroundColor=[UIColor clearColor];
    [self.imageNumLabel setTextColor:[UIColor whiteColor]];
    self.backBtn.imageView.contentMode=UIViewContentModeScaleAspectFit;
    [self.backBtn setImage:[UIImage imageNamed:@"back-White-Normal"] forState:UIControlStateNormal];
    [self.backBtn setImage:[UIImage imageNamed:@"back-White-TouchDown"] forState:UIControlStateHighlighted];
}

-(void)configCollectionView{
    
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    self.collectionView.pagingEnabled=YES;
    self.collectionView.showsHorizontalScrollIndicator=NO;
    self.collectionView.showsVerticalScrollIndicator=NO;

    [self.view addSubview:self.collectionView];
}

-(void)configTextField{
    
    self.desc.backgroundColor=[UIColor clearColor];
    [self.desc setTextColor:[UIColor whiteColor]];
    self.desc.font=[UIFont systemFontOfSize:15];
}

-(void)configBottomView{
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){1,1,1,1});
    //common btn
    self.imagePropertyBtn.contentMode=UIViewContentModeScaleToFill;
    [self.imagePropertyBtn setImage:[UIImage imageNamed:@"imagePropertyBtnNormal"] forState:UIControlStateNormal];
    [self.imagePropertyBtn setImage:[UIImage imageNamed:@"imagePropertyBtnTouchDown"] forState:UIControlStateHighlighted];
    
    self.bottomView.backgroundColor=[UIColor colorWithWhite:0 alpha:1.0];
    
    self.btnA.imageView.contentMode=UIViewContentModeScaleToFill;
    self.btnA.layer.cornerRadius=self.btnA.bounds.size.height/2.0;
    [self.btnA.layer setBorderWidth:1.0];
    [self.btnA.layer setBorderColor:colorref];
    
    self.btnB.layer.cornerRadius=self.btnB.bounds.size.height/2.0;
    [self.btnB setTitle:[NSString stringWithFormat:@"%ld跟贴",(long)self.commentCount] forState:UIControlStateNormal];
    self.btnB.imageView.contentMode=UIViewContentModeScaleToFill;
    [self.btnB setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    //diff btn
    if(self.vcstyle==photoExhibitionDetailVC){
        
        [self.btnA setImage:[UIImage imageNamed:@"shareNormal"] forState:UIControlStateNormal];
        self.imagePropertyBtn.hidden=YES;
        
    }else{
        
        [self.btnA setTitle:@"投他一票" forState:UIControlStateNormal];
        [self.btnA setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.btnA setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        
        [self.btnA setTitle:@"已投票" forState:UIControlStateDisabled];
        [self.btnA setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        
        [self.btnA setBackgroundColor:[UIColor clearColor]];
        [self.btnA setImage:nil forState:UIControlStateNormal];
        [self.btnA setImage:nil forState:UIControlStateSelected];
    }
}

-(void)configImagePropertyView{
    
    [self.view addSubview:self.imagePropertyView];
}

-(void)configImagePropertyViewWithModel:(imagePropertyModel *)model{
    
    [self.imagePropertyView configLabelWithImagePropertyModel:model];
}

-(void)configLabel{
    
    [self.btnB setTitle:[NSString stringWithFormat:@"%ld跟贴",(long)self.commentCount] forState:UIControlStateNormal];
    self.desc.text=[self descTextWithIndex:0];
    //image num label
    if(self.vcstyle==photoExhibitionDetailVC && self.photosURLArray.count>=2){
        
        self.imageNumLabel.text=[NSString stringWithFormat:@"1/%lu",(unsigned long)self.photosURLArray.count];
        
    }else{
        
        self.imageNumLabel.hidden=YES;
    }
}

-(void)scrollConfigLabelWithIndex:(NSInteger)index{
    
    self.desc.text=[self descTextWithIndex:index];
    [self configTextField];
}

-(NSString *)descTextWithIndex:(NSInteger)index{
    
    NSString *descText;
    
    if(self.vcstyle==photoExhibitionDetailVC){
        
        if(self.photosDictArray.count==0){
            
            self.desc.hidden=YES;
            return descText;
        }
        
        if([NSString isEmptyString:(((photosModel *)self.photosDictArray[index]).desc)]){
            
            self.desc.hidden=YES;
            
        }else{
            
            self.desc.hidden=NO;
            descText=(((photosModel *)self.photosDictArray[index]).desc);
        }
        
    }else{
        
        if([NSString isEmptyString:(self.photoMatchDetailModel.desc)]){
            
            self.desc.hidden=YES;
            
        }else{
            
            self.desc.hidden=NO;
            descText=self.photoMatchDetailModel.desc;
        }
    }
        
    return descText;
}

#pragma mark - may need method
-(void)calculateTextHeightWithText:(NSString *)text{
    
    CGSize textSize = [text boundingRectWithSize:CGSizeMake(SCREEN_W, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;

    if((textSize.height+10)<self.descConstraintH.constant){
        
        self.descConstraintH.constant=textSize.height+10;
    }
}

-(void)bringBottomViewAndDescToTop{
    
    [self.view bringSubviewToFront:self.bottomView];
    [self.view bringSubviewToFront:self.desc];
}

-(void)configVotedBtnA{
    
    [self.btnA setBackgroundColor:[UIColor whiteColor]];
    self.btnA.enabled=NO;
}

#pragma mark - fetch data method
-(void)fetchPhotoUserAlbumDetail
{
    if([NSString isEmptyString:self.photoMatchDetailUUID]){
        return;
    }
    [GMTipsHelper showLoading];
    PCUserModel *userModel = [[PCUserInfoManager sharedUserInfo] readUserInfo];
    NSDictionary *pDic = @{@"uuid":self.photoMatchDetailUUID,@"token":userModel.userToken};
    
    [[fetchData shared] fetchUserAlbumDetailWith:pDic SuccessBlock:^(NSMutableArray *gamelistArray) {
        
//        [GMTipsHelper showDismiss];
        [self.photosURLArray removeAllObjects];
        [self.photosURLArray addObjectsFromArray:gamelistArray];
        self.imageNumLabel.text=[NSString stringWithFormat:@"1/%lu",(unsigned long)self.photosURLArray.count];

        [self.collectionView reloadData];

    } failBlock:^{
        [GMTipsHelper showDismiss];
        [GMTipsHelper showBadNetwork];
    }];
}

-(void)fetchPhotoExhibitionDetail{
    
    [GMTipsHelper showLoading];
    
    [[fetchData shared] fetchExhibitionDetailWithParameterUUID:self.photoExhibitionDetailUUID successBlock:^(photoExhibitionDetailModel *photoExhibitionDetail) {
        
        if ([photoExhibitionDetail.photoModelArray count] == 0) {
            [GMTipsHelper showDismiss];
        }
        self.photoExhibitionDetailModel=photoExhibitionDetail;
        self.commentCount=photoExhibitionDetail.comments_count;
        self.photosDictArray=photoExhibitionDetail.photoModelArray;
        
        [self.photosURLArray removeAllObjects];
        for(photosModel *pM in self.photosDictArray){
            [self.photosURLArray addObject:pM.imageURL];
        }
        
        [self showShareButton];
        [self configLabel];
        [self configTextField];
        [self.collectionView reloadData];
        [self bringBottomViewAndDescToTop];
        
    } failBlock:^{
        
        [GMTipsHelper showDismiss];
        [GMTipsHelper showBadNetwork];
    }];
}

// 显示分享按钮
-(void)showShareButton
{
    shareModel *sModel = ((shareModel *)[self.photoExhibitionDetailModel.shareModelArray firstObject]);
    if (sModel) {
        self.btnA.hidden = NO;
    }
}

-(void)fetchphotoMatchDetail{
    
    if([NSString isEmptyString:self.photoMatchDetailUUID]){
        return;
    }
    
    id token=[self tokenObj];

    if ([[PCUserInfoManager sharedUserInfo] checkUserInfo]) {
        token = ([[PCUserInfoManager  sharedUserInfo] readUserInfo]).userToken;
    }
    NSDictionary *dict=@{@"uuid":self.photoMatchDetailUUID,@"token":token};
    [GMTipsHelper showLoading];
    [[fetchData shared] fetchMatchDetailWithParameterDict:dict successBlock:^(gameDetailModel *matchDetailModel) {
        
//        [GMTipsHelper showDismiss];
        self.photoMatchDetailModel=matchDetailModel;
        self.imagePropertyModel=matchDetailModel.imagePropertyModel;
        [self.photosURLArray removeAllObjects];
        self.commentCount= matchDetailModel.commentsCount;
        [self.photosURLArray addObject:matchDetailModel.imageURL];
        
        [self configImagePropertyViewWithModel:matchDetailModel.imagePropertyModel];
        [self configLabel];
        [self configTextField];
        [self.collectionView reloadData];
        [self bringBottomViewAndDescToTop];
        
        if(matchDetailModel.voted){
            [self configVotedBtnA];
        }
        
    } failBlock:^{
        [GMTipsHelper showDismiss];
        [GMTipsHelper showBadNetwork];
    }];
}

-(void)voteThisGuy{
    
    NSString *token=([[PCUserInfoManager  sharedUserInfo] readUserInfo]).userToken;
    id uuid;
    
    if([NSString isEmptyString:token]){
        //提示登录
        [self loginPrompt];
        return;
    }
    
    if([NSString isEmptyString:self.photoMatchDetailUUID]){
        
        uuid=[NSNull null];
        
    }else{
        
        uuid=self.photoMatchDetailUUID;
    }
    
    NSDictionary *parameterDict=@{@"token":token,@"uuid":uuid};
    
    [[fetchData shared] postVoteWithParameterDict:parameterDict successBlock:^(NSInteger voteStateCode) {
        
        if(voteStateCode==API_RESPONSE_OK){
            
            [self configVotedBtnA];
        }
        
    } failBlock:^{
        
        [GMTipsHelper showBadNetwork];
    }];
}

#pragma mark - collectionview delegate,datasource,flowdelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.photosURLArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
        
    [self.cell configCellView];
    self.cell.scrollView.delegate=self;
    
    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTap.numberOfTapsRequired=2;
    self.cell.imageView.tag=indexPath.item;
    [self.cell.imageView addGestureRecognizer:doubleTap];
    
    [self.cell configCellWithImageURL:self.photosURLArray[indexPath.item]];
    isClickScaled=NO;
    
    return self.cell;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_W, SCREEN_H);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //如果是imageScrollView滚动，则return
    if([scrollView isMemberOfClass:[UIScrollView class]]){
        
        return;
    }
    
    isClickScaled=NO;
    NSInteger index=self.collectionView.contentOffset.x/SCREEN_W;
    self.imageNumLabel.text=[NSString stringWithFormat:@"%ld/%lu",(index +1),(unsigned long)self.photosURLArray.count];
    
    [self scrollConfigLabelWithIndex:index];
//    [self.cell configCellView];
    [self.cell configCellWithImageURL:self.photosURLArray[index]];
}

#pragma mark - button action
- (IBAction)backAction:(UIButton *)sender {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:PHOTOCOMMENTCOUNT_NOTIFICATION_NAME object:nil];
    [GMTipsHelper showDismiss];
    [UIApplication sharedApplication].statusBarHidden=NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnAAction:(UIButton *)sender{
    
    if(self.vcstyle==photoExhibitionDetailVC){
        //转发图片
        shareModel *sModel = (shareModel *)[self.photoExhibitionDetailModel.shareModelArray firstObject];
        if (sModel) {
            [[ShareSDKHelper sharedMultiPlatform] publishTitle:sModel.title andContent:sModel.summary andImageURL:sModel.imageURL andUrl:sModel.url andDescription:sModel.summary];
        }
    }
    
    if(self.vcstyle==photoMatchDetailVC){
        //赞这张图片
        [self voteThisGuy];
    }
}

- (IBAction)btnBCommentListAction:(UIButton *)sender {
    //发帖跳转
    UIStoryboard *commentStory=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    PCCommentController *commentVC=[commentStory instantiateViewControllerWithIdentifier:@"PCCommentController"];
    NSString *alBumId=([NSString isEmptyString:self.photoExhibitionDetailUUID] ? self.photoMatchDetailUUID : self.photoExhibitionDetailUUID);
    NSString *itemType=([NSString isEmptyString:self.photoExhibitionDetailUUID] ? @"GamePhoto" : @"Exhibition");
    
    commentVC.albumId=alBumId;
    commentVC.item_type=itemType;
    
    [self.navigationController pushViewController:commentVC animated:YES];
}

- (IBAction)imagePropertyBtnAction:(UIButton *)sender {
    
        [UIView animateWithDuration:0.3 animations:^{
            
            [self.view bringSubviewToFront:self.imagePropertyView];
            
            self.imagePropertyView.frame=CGRectMake(0, SCREEN_H-imagePropertyViewH, SCREEN_W, imagePropertyViewH);
        }];
}

#pragma mark - property
-(imagePropertyView *)imagePropertyView{
    
    if(_imagePropertyView==nil){
        
        UINib *nib=[UINib nibWithNibName:@"imagePropertyView" bundle:[NSBundle mainBundle]];
        _imagePropertyView=[[nib instantiateWithOwner:nil options:nil] firstObject];
        _imagePropertyView.frame=CGRectMake(0, SCREEN_H+imagePropertyViewH, SCREEN_W, imagePropertyViewH);
    }
    
    return _imagePropertyView;
}

-(id)tokenObj{
    
    id tokenObj;
    if([NSString isEmptyString:self.token]){
        tokenObj=[NSNull null];
    }else{
        tokenObj=self.token;
    }
    return tokenObj;
}

-(UICollectionView *)collectionView{
    
    if(_collectionView==nil){
        
        CGRect rect=CGRectMake(0, 0, SCREEN_W, SCREEN_H);
        UICollectionViewFlowLayout *flow=[[UICollectionViewFlowLayout alloc] init];
        flow.scrollDirection=UICollectionViewScrollDirectionHorizontal;
        _collectionView=[[UICollectionView alloc] initWithFrame:rect collectionViewLayout:flow];
        
        [_collectionView registerClass:[photoCommCell class] forCellWithReuseIdentifier:cellID];
    }
    
    return _collectionView;
}

-(NSMutableArray *)photosURLArray{
    
    if(_photosURLArray==nil){
        
        _photosURLArray=[[NSMutableArray alloc]initWithCapacity:8];
    }
    return _photosURLArray;
}

-(void)loginPrompt{
    
    if (iOS(8.0)) {
        
        UIAlertController * cancleAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请登录" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //跳转到个人中心
            [self jumpToLogin];
        }];
        
        UIAlertAction *cancleAcion = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [cancleAlert addAction:okAciton];
        [cancleAlert addAction:cancleAcion];
        [self presentViewController:cancleAlert animated:YES completion:^{
            
        }];
        
    }else{
        
        UIAlertView *cancleAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        //cancleAlert.tag =CancleTag;
        cancleAlert.delegate = self;
        [cancleAlert show];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        //跳转到个人中心
        [self jumpToLogin];
        
    }else{
        
        
    }
}

-(void)jumpToLogin{
    
    UIStoryboard *loginS=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];;
    PCUserLoginViewController *loginVC=[loginS instantiateViewControllerWithIdentifier:@"PCUserLoginVC"];
//
    [self.navigationController pushViewController:loginVC animated:YES];
}

#pragma mark - zoom method
-(void)handleDoubleTap:(UITapGestureRecognizer *)recognizer{
    
    NSIndexPath *indexPath=[NSIndexPath indexPathForItem:recognizer.view.tag inSection:0];
    photoCommCell *cell=[self visibleCellWithIndexPath:indexPath];
    //双击缩放
    CGFloat newScale;
    CGRect zoomRect;
    
    if(!isClickScaled){
        
        self.backRect=recognizer.view.frame;
        newScale = cell.scrollView.zoomScale * 10.0;
        zoomRect = [self zoomRectForScale:newScale withCenter:[recognizer locationInView:recognizer.view]];
        
        [cell.scrollView zoomToRect:zoomRect animated:YES];
        isClickScaled=YES;
        
    }else{
        
        [cell.scrollView zoomToRect:self.backRect animated:YES];
        isClickScaled=NO;
    }
}

//缩放点击处的rect
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center{
    
    CGRect zoomRect;
    CGSize size=CGSizeMake(SCREEN_W, SCREEN_H);
    zoomRect.size.height = size.height / scale;
    zoomRect.size.width  = size.width / scale;
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}

#pragma mark - scrollview delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return [scrollView.subviews lastObject];
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    //缩放因子改变了
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
    (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    ((UIImageView *)[scrollView.subviews lastObject]).center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
                                                                 scrollView.contentSize.height * 0.5 + offsetY);
}

//返回visible cell
-(photoCommCell *)visibleCellWithIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell=[self.collectionView cellForItemAtIndexPath:indexPath];
    
    return (photoCommCell *)cell;
}

@end
